package interaction.game;

import java.awt.Rectangle;

import processing.core.*;

public class CollectibleItem extends RandomForegroundItem {
	private boolean collected = false;
	
	private float floatDirection = 1; 
	private int floatStepper = 0;
	private final int FLOAT_VARIANCE = 30;
	private ScoreItem scores;
	
	private PImage normalImage;
	private PImage collectedImage;
	
	public CollectibleItem(PImage normalImage, PImage collectedImage, PVector position, PVector velocity, int loopDistance, Hero hero, ScoreItem scores) {
		super(normalImage, position, velocity, loopDistance, hero);
		this.normalImage = normalImage;
		this.collectedImage = collectedImage;
		this.scores = scores;
	}
	
	public void resetPosition() {
		super.resetPosition();
		getPosition().y = generateRandomHeight(); 
		// Becomes collectable again after falling offscreen
		setCollected(false);
	}
	
	public void setCollected(boolean collected)
	{
		this.collected = collected;
		if (collected) {
			setImage(collectedImage);
		} else {
			setImage(normalImage);
		}
	}
	
	public float generateRandomHeight()
	{
		return getInitialPosition().y + (float) (50*getRandom().nextGaussian());
	}
	
	public int generateLoopOffset() {
		return (100+(int) (500*Math.abs(getRandom().nextGaussian())));
	}
	
	public void update()
	{
		super.update();
		
		if (!collected) {
			applyFloat();
			
			if (intersectsWithHero()) {
				collect();
			}
		}
	}
	
	public void applyFloat()
	{
		PVector position = getPosition();
		
		if (Math.abs(this.floatStepper) < FLOAT_VARIANCE){
			position.y -=  this.floatDirection;
			this.floatStepper++;
		} else { //change direction
			this.floatDirection = -this.floatDirection;
			this.floatStepper = 0;
		}
	}
	
	public void collect(){
		scores.addScore(500);
		setCollected(true);
	}
}
